// \src-autox\tiktok\index.ts
const { printLog, humanLikeSwipe, screenshot, humanLikeGesture, mainThreadsSleep: uiSleep } = require('../utils/utils')
const { treasureBoxLogic, popupWindow } = require('./operation-logic')
declare const device: { width: number; height: number };
const scriptEngine = engines.myEngine()
const data:any = engines.myEngine().execArgv.data

console.log(data);

let deviceInfo = {
  width: device.width,
  height: device.height
}
// 
const totalTime: number = 200 * 60 * 1000
let videoTime: number = 0
let pursePos:{x: number, y:number} = {x: 0, y: 0}
let purseTime: number = 0
let currentDate:number = new Date().getTime()
let appStart: boolean = launch('com.ss.android.ugc.aweme.lite')
console.log(appStart);
console.log(treasureBoxLogic);

if(appStart){
  printLog( '已启动')
  
}else{
  printLog('未检测到应用')
  printLog({text: '停止运行当前脚本', color: '#ff0000'})
  scriptEngine.forceStop()
}
let bool = text('首页').findOne(5000)
if(bool){
  printLog('获取钱袋位置')
  let friendUi = text('朋友').findOnce()
  let friendUiRect = friendUi?.bounds()
  let infoUi = text('消息').findOnce()
  let infoUiRect = infoUi?.bounds()
  friendUiRect
  pursePos.x = friendUiRect.right + (infoUiRect.left - friendUiRect.right) / 2
  pursePos.y = friendUiRect.centerY()
  printLog(`${pursePos.x}, ${pursePos.y}`)

}else{
  printLog('未检测到首页')
  printLog({text: '停止运行当前脚本', color: '#ff0000'})
  scriptEngine.forceStop()
}
// printLog('获取当前金币')

sleep(10000)
// 首次启动弹窗处理
popupWindow()
if(textStartsWith('立即签到').exists()){
  printLog('检测到签到页面')
  let rect = textStartsWith('立即签到').findOnce()?.bounds()
  if(rect){
    humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
  }
  printLog('已签到')
  sleep(2000)
  popupWindow()
  sleep(2000)
}

let x1 = deviceInfo.width/2
let x2 = deviceInfo.width/2
let y1 = deviceInfo.height / 2 + deviceInfo.height / 2 / 2
let y2 = deviceInfo.height / 2
// printLog('开始滑动')
// humanLikeSwipe(x1, y1, x2, y2, 100)
let n = 0
while (true) {
  let bool = text('开宝箱').exists()
  console.log(bool , !purseTime , new Date().getTime() - currentDate > purseTime);
  
  if(bool || !purseTime || new Date().getTime() - currentDate > purseTime){
    printLog('前往钱袋')
    humanLikeGesture(pursePos.x - 20, pursePos.y - 20, pursePos.x + 20, pursePos.y + 20)
    sleep(2000)
    purseTime = treasureBoxLogic()
    currentDate = new Date().getTime()
    sleep(2000)
    printLog('开始滑动')
  }
  let s = random(15000, 30000)
  printLog(`${s / 1000}`)
  
  videoTime = videoTime + s;
  // x1 = random(deviceInfo.width * 0.2, deviceInfo.width * 0.8)
  // x2 = random(deviceInfo.width * 0.2, deviceInfo.width * 0.8)
  humanLikeSwipe(x1, y1, x2, y2, 200)

  // 图片视频无需停留 


  sleep(s)

  if(totalTime < videoTime){
    printLog('停止运行')
    scriptEngine.forceStop()
    break;
  }
}

